package com.framework.modules.base.controller;

import java.util.*;

import cn.hutool.dfa.SensitiveUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.common.annotation.Login;
import com.framework.common.annotation.SensitiveFilter;
import com.framework.common.config.SystemConfigName;
import com.framework.common.utils.HttpContextUtils;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.QuickHelper;
import com.framework.modules.base.entity.PostsCommentsEntity;
import com.framework.modules.base.entity.UserDoingEntity;
import com.framework.modules.base.entity.UserInfoEntity;
import com.framework.modules.base.service.PostsCommentsService;
import com.framework.modules.base.service.UserDoingService;
import com.framework.modules.base.service.UserInfoService;
import com.framework.modules.dataview.entity.ViewUserPostsListEntity;
import com.framework.modules.dataview.service.ViewUserInfoCommentsService;
import com.framework.modules.dataview.service.ViewUserPostsListService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.framework.modules.base.service.PostsService;
import com.framework.common.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户动态
 *
 * @author MIT
 * @email litmiss@gmail.com
 * @date 2020-10-03 16:58:28
 */
@RestController
@RequestMapping("/posts")
@Api(tags = "动态接口")
public class PostsController {
    @Autowired
    private PostsService postsService;

    @Autowired
    private PostsCommentsService postsCommentsService;

    @Autowired
    private ViewUserInfoCommentsService viewUserInfoCommentsService;

    @Autowired
    private ViewUserPostsListService viewUserPostsListService;

    @Autowired
    private UserDoingService userDoingService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 广场动态
     * [ 广场动态 ] tb_user_info ，tb_posts
     * @param tag 标签
     * @param type 类型 post,video
     * @param nickname 昵称
     * @return 返回成功
     */
    @Login
    @GetMapping("/get_space_post")
    @ApiOperation("广场动态")
    public R getSpacePost(Integer page,@RequestParam(required = false)String tag,
                          @RequestParam(required = false)String type,
                          @RequestParam(required = false)String nickname){
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("page",page.toString());
        paramMap.put("limit",QuickHelper.getConfigString(SystemConfigName.RECORD_POST_PAGE_COUNT));
        if(StringUtils.isNotBlank(tag)){
            paramMap.put("tag",tag);
        }
        if(StringUtils.isNotBlank(type)){
            paramMap.put("type",type);
        }
        if(StringUtils.isNotBlank(nickname)){
            paramMap.put("nickname",nickname);
        }
        List<String> viewPolicys=new ArrayList<>();
        viewPolicys.add("public");
        paramMap.put("viewPolicys",viewPolicys);
        paramMap.put("state",1);
        Object object=viewUserPostsListService.queryPage(paramMap);
        if(object instanceof String){
            return R.error(object.toString());
        }
        return R.ok(object);
    }

    /**
     * 我关注的人的动态
     * [ 我关注的人的动态 ] tb_user_info ，tb_posts
     * @param tag 标签
     * @param type 类型 post,video
     * @param nickname 昵称
     * @return 返回成功
     */
    @Login
    @GetMapping("/get_my_attent_post")
    @ApiOperation("我关注的人的动态")
    public R getMyAttentPost(Integer page,@RequestParam(required = false)String tag,
                          @RequestParam(required = false)String type,
                          @RequestParam(required = false)String nickname){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("page",page.toString());
        paramMap.put("limit",QuickHelper.getConfigString(SystemConfigName.RECORD_POST_PAGE_COUNT));
        if(StringUtils.isNotBlank(tag)){
            paramMap.put("tag",tag);
        }
        if(StringUtils.isNotBlank(type)){
            paramMap.put("type",type);
        }
        if(StringUtils.isNotBlank(nickname)){
            paramMap.put("nickname",nickname);
        }
        List<String> viewPolicys=new ArrayList<>();
        viewPolicys.add("public");
        viewPolicys.add("friend");
        paramMap.put("viewPolicys",viewPolicys);
        paramMap.put("state",1);
        paramMap.put("uids",userDoingService.getMyAttentUserUid(uid.intValue()));
        Object object=viewUserPostsListService.queryPage(paramMap);
        if(object instanceof String){
            return R.error(object.toString());
        }
        return R.ok(object);
    }


    /**
     * 我喜欢的人的动态
     * [ 我喜欢的人的动态] tb_user_info ，tb_posts
     * @param tag 标签
     * @param type 类型 post,video
     * @param nickname 昵称
     * @return 返回成功
     */
    @Login
    @GetMapping("/get_my_like_post")
    @ApiOperation("我喜欢的人的动态")
    public R getMyLikePost(Integer page,@RequestParam(required = false)String tag,
                          @RequestParam(required = false)String type,
                          @RequestParam(required = false)String nickname){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("page",page.toString());
        paramMap.put("limit",QuickHelper.getConfigString(SystemConfigName.RECORD_POST_PAGE_COUNT));
        if(StringUtils.isNotBlank(tag)){
            paramMap.put("tag",tag);
        }
        if(StringUtils.isNotBlank(type)){
            paramMap.put("type",type);
        }
        if(StringUtils.isNotBlank(nickname)){
            paramMap.put("nickname",nickname);
        }
        List<String> viewPolicys=new ArrayList<>();
        viewPolicys.add("public");
        paramMap.put("viewPolicys",viewPolicys);

        paramMap.put("uids",userDoingService.getMyLikeUserUid(uid.intValue()));
        paramMap.put("state",1);
        Object object=viewUserPostsListService.queryPage(paramMap);
        if(object instanceof String){
            return R.error(object.toString());
        }
        return R.ok(object);
    }

    /**
     * 喜欢我的人的动态
     * [ 喜欢我的人的动态] tb_user_info ，tb_posts
     * @param tag 标签
     * @param type 类型 post,video
     * @param nickname 昵称
     * @return 返回成功
     */
    @Login
    @GetMapping("/get_like_me_post")
    @ApiOperation("喜欢我的人的动态")
    public R getLikeMePost(Integer page,@RequestParam(required = false)String tag,
                         @RequestParam(required = false)String type,
                         @RequestParam(required = false)String nickname){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("page",page.toString());
        paramMap.put("limit",QuickHelper.getConfigString(SystemConfigName.RECORD_POST_PAGE_COUNT));
        if(StringUtils.isNotBlank(tag)){
            paramMap.put("tag",tag);
        }
        if(StringUtils.isNotBlank(type)){
            paramMap.put("type",type);
        }
        if(StringUtils.isNotBlank(nickname)){
            paramMap.put("nickname",nickname);
        }
        List<String> viewPolicys=new ArrayList<>();
        viewPolicys.add("public");
        paramMap.put("viewPolicys",viewPolicys);

        paramMap.put("uids",userDoingService.getLikeMeUserUid(uid.intValue()));
        paramMap.put("state",1);
        Object object=viewUserPostsListService.queryPage(paramMap);
        if(object instanceof String){
            return R.error(object.toString());
        }
        return R.ok(object);
    }


    /**
     * 喜欢评论
     * [ 喜欢评论] tb_user_info ，tb_user_doing
     * @param objectUid 评论对象Uid
     * @param objectId 评论id
     * @return 返回成功 result true=喜欢 false=非喜欢
     */
    @Login
    @PostMapping("/comment_item_like")
    @ApiOperation("喜欢评论")
    public R commentItemLike(Integer objectUid,Integer objectId){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        UserDoingEntity userDoingEntity=new UserDoingEntity();
        userDoingEntity.setUid(uid.toString());
        //对象名称 user,post,comment
        userDoingEntity.setObjectName("comment");
        userDoingEntity.setObjectId(objectId);
        userDoingEntity.setObjectUid(objectUid.toString());
        userDoingEntity.setType("like");
        Object object=postsCommentsService.likeComment(userDoingEntity);
        if(object instanceof String){
            return R.error(object.toString());
        }
        return R.ok("操作成功").put("result",object);
    }


    /**
     * 动态留言
     * [ 动态留言] 相关表 tb_posts_comments
     * @param postId 动态id
     * @param nickname 回复人
     * @param content 评论内容
     * @param parentId 回复的评论id 默认为0
     * @return 返回成功
     */
    @Login
    @PostMapping("/comment_item_add")
    @ApiOperation("动态留言")
    public R commentItemAdd(Integer postId,String nickname,String content,@RequestParam(defaultValue = "0") Integer parentId){
        Long uid = (Long) HttpContextUtils.getHttpServletRequestHeaderValue("uid");
        //检测敏感词
        if(SensitiveUtil.containsSensitive(content)){
            R.error("你的评论包含敏感词");
        }
        PostsCommentsEntity postsCommentsEntity=new PostsCommentsEntity();
        postsCommentsEntity.setUid(uid.toString());
        postsCommentsEntity.setContent(content);
        if(parentId!=0){
            postsCommentsEntity.setParentName(nickname);
        }
        postsCommentsEntity.setParentId(parentId);
        postsCommentsEntity.setPostId(postId);
        Object object=postsCommentsService.commentItemAdd(postsCommentsEntity);
        if(object instanceof String){
            return R.error(object.toString());
        }
        return R.ok("评论成功");
    }

    /**
     * 分享动态
     * [ 分享动态] tb_posts
     * @param objectId 动态id
     * @return 返回成功
     */
    @Login
    @PostMapping("/share")
    @ApiOperation("分享动态")
    public R sharePosts(Integer objectId){
        postsService.updatePostsShareCount(objectId,null,"add");
        return R.ok("分享成功");
    }



    /**
     * 获取评论
     * [ 喜欢的动态 ] tb_posts_comments
     * @param postId 动态id
     * @return 返回成功
     */
    @Login
    @GetMapping("/get_comment_list")
    @ApiOperation("获取评论")
    public R getCommentList(Integer page,String postId){
        if(StringUtils.isBlank(postId)){
            return R.error("参数出错");
        }
        Map<String,Object> para=new HashMap<>();
        para.put("page",page.toString());
        para.put("limit",QuickHelper.getConfigString(SystemConfigName.RECORD_POST_PAGE_COUNT));
        para.put("postId",postId);
        para.put("state",1);
        PageUtils pageObj=viewUserInfoCommentsService.queryPage(para);
        return R.ok(pageObj);
    }


    /**
     * 获取动态详细信息
     * [ 喜欢的动态 ] tb_posts
     * @param postId 动态id
     * @return 返回成功
     * postInfo 动态信息
     * likePostUserInfos 喜欢动态的人信息
     */
    @Login
    @GetMapping("/get_post_item_info")
    @ApiOperation("获取动态详细信息")
    public R getPostItemInfo(String postId){
        if(StringUtils.isBlank(postId)){
            return R.error("参数出错");
        }
        ViewUserPostsListEntity viewUserPostsListEntitie=viewUserPostsListService.selectOne(new EntityWrapper<ViewUserPostsListEntity>().eq("id",postId));
        if(viewUserPostsListEntitie==null){
            return R.error("没有此条动态");
        }
        String likeUids=viewUserPostsListEntitie.getLikeUids();
        List<String> likeUidList;
        List<UserInfoEntity> userInfoEntityList=new ArrayList<>();
        if(StringUtils.isNotBlank(likeUids)){
            likeUidList= Arrays.asList(likeUids.split(","));
            int index=likeUidList.size()>10?10:likeUidList.size();
            userInfoEntityList=userInfoService.selectList(new EntityWrapper<UserInfoEntity>().in("uid",likeUidList.subList(0,index)));
        }
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("postInfo",viewUserPostsListEntitie);
        resultMap.put("likePostUserInfos",userInfoEntityList);
        return R.ok(resultMap);
    }

}
